package team.zero.lemon.common.service;

import java.util.List;

import team.zero.lemon.common.config.Global;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.mapper.JsonMapper;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.persistence.ICrudDao;
import team.zero.lemon.common.persistence.DataEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service基类
 *
 * @author bolin
 * @version 2014-05-16
 */
public abstract class CrudService<D extends ICrudDao<T>, T extends DataEntity> extends BaseService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 持久层对象
     */
    @Autowired
    protected D dao;

    /**
     * 获取单条数据
     *
     * @param id
     * @return
     */

    public T findById(int id) {
        try {
            return dao.findById(id);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 查询列表数据
     *
     * @param entity
     * @return
     */
    public List<T> find(T entity) {
        try {
            return dao.findList(entity);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 查询所有列表数据
     *
     * @param
     * @return
     */

    public List<T> findAll() {
        try {
            return dao.findAll();
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 查询分页数据
     *
     * @param entity
     * @return
     */
    public Page<T> findPage(T entity) {
        try {
            entity.page.setList(dao.findPage(entity));
            return entity.page;
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param entity
     */
    public int save(T entity) {
        try {
            if (entity.isInsert()) {
                entity.preInsert();
                if (dao.insert(entity) > 0) {
                    return entity.getId();
                } else {
                    return 0;
                }
            } else {
                entity.preUpdate();
                return dao.update(entity);
            }
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param entitys
     */
    public void save(List<T> entitys) {
        try {
            entitys.forEach(entity -> {
                if (entity.isInsert()) {
                    entity.preInsert();
                    dao.insert(entity);
                } else {
                    entity.preUpdate();
                    dao.update(entity);
                }
            });
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param entity
     */
    public int saveWithId(T entity) {
        try {
            if (entity.isInsert()) {
                entity.preInsert();
                if (dao.insertWithKey(entity) > 0) {
                    return entity.getId();
                } else {
                    return 0;
                }
            } else {
                entity.preUpdate();
                return dao.update(entity);
            }
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    public int delete(int id) {
        try {
            return dao.delete(id);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 逻辑删除数据
     *
     * @param entity
     */

    @Transactional
    public int deleteLogic(T entity) {
        try {
            entity.preDelete();
            return dao.deleteLogic(entity);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 删除数据
     *
     * @param ids
     */

    @Transactional
    public int deleteMulti(List<Integer> ids) {
        try {
            return dao.deleteMulti(ids);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 逻辑删除数据
     *
     * @param entity
     */

    @Transactional
    public int deleteLogicMulti(T entity) {
        try {
            entity.preDelete();
            return dao.deleteLogicMulti(entity);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


}
